package com.workcell.workdata.xthings.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.CodeChainnumUseStateEnum;
import com.workcell.workdata.xthings.entity.CodeChainnum;
import com.workcell.workdata.xthings.entity.CodeChainnumUseStateData;
import com.workcell.workdata.xthings.mapper.CodeChainnumUseStateDataMapper;
import com.workcell.workdata.xthings.service.CodeChainnumService;
import com.workcell.workdata.xthings.service.CodeChainnumUseStateDataService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 二维码码库
 *
 * @author guoyu
 * @date 2023-25- 09:34:00
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CodeChainnumUseStateDataServiceImpl extends ServiceImpl<CodeChainnumUseStateDataMapper, CodeChainnumUseStateData> implements CodeChainnumUseStateDataService {

    private final CodeChainnumService codeChainnumService;
    @Override
    public void add(LocalDateTime localDateTime) {
        //入库
        LambdaQueryWrapper<CodeChainnum> onlineCondtion = new QueryWrapper<CodeChainnum>().lambda()
                .eq(CodeChainnum::getUseState, CodeChainnumUseStateEnum.ONLINE.getCode());
        List<CodeChainnum> onlineList = codeChainnumService.list(onlineCondtion);
        List<CodeChainnumUseStateData> useStateData = Lists.newArrayList();
        onlineList.forEach(item ->{
            CodeChainnumUseStateData data = CodeChainnumUseStateData.builder()
                    .tcId(item.getId())
                    .deviceNo(item.getDeviceNo())
                    .manufacturerCode(item.getManufacturerCode())
                    .hardwareType(item.getHardwareType())
                    .useState(item.getUseState())
                    .scheduledTime(localDateTime)
                    .build();
            useStateData.add(data);
        });

        LambdaQueryWrapper<CodeChainnum> offlineCondtion = new QueryWrapper<CodeChainnum>().lambda()
                .eq(CodeChainnum::getUseState, CodeChainnumUseStateEnum.OFFLINE.getCode());
        List<CodeChainnum> offlineList = codeChainnumService.list(offlineCondtion);
        offlineList.forEach(item ->{
            CodeChainnumUseStateData data = CodeChainnumUseStateData.builder()
                    .tcId(item.getId())
                    .deviceNo(item.getDeviceNo())
                    .manufacturerCode(item.getManufacturerCode())
                    .hardwareType(item.getHardwareType())
                    .useState(item.getUseState())
                    .scheduledTime(localDateTime)
                    .build();
            useStateData.add(data);
        });
        if(this.saveBatch(useStateData)){
            log.info(useStateData.size()+":设备每点上下线处理入库{}成功。",useStateData);
        }
    }

    @Override
    public List<CodeChainnumUseStateData> getCountByTypeAndTime(String hardwareType, Date scheduledTime) {
        return this.baseMapper.getCountByTypeAndTime(hardwareType, scheduledTime);
    }
}
